{include file="common/header" /}
<script src="__STATIC__/index/js/shopping_cart.js"></script>
<body>
<head>
 {include file="common/personal_top" /}
<!--用户中心样式-->

<div class="catbox">
<div class=" clear bread" style="padding-bottom: 20px;font-size: 14px;color:#666;">当前位置：<a href="#">首页</a> <span class="pipe">&gt;</span> 购物车</div>
<div class="carImg" style="padding-bottom: 10px;text-align: center">
  <img src="__STATIC__/index/images/car1.jpg" alt="">
</div>
    <table id="cartTable">
      <thead>
        <tr>
          <th><label><input class="check-all check" type="checkbox"/>&nbsp;全选</label></th>
          <th>商品</th>
          <th>单价</th>
          <th>数量</th>
          <th>小计</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
      {volist name="cars" id="vo"}
        <tr>
          <td class="checkbox"><input class="check-one check" name="id[]" value="{$vo.gid}" type="checkbox"/></td>
          <td class="goods"><img src="{$vo.picture}" alt=""/><span>{$vo.gname}</span></td>
          <td class="price">{$vo.price}</td>
          <td class="count"><span class="reduce"></span><input class="count-input" type="text" value="{$vo.num}"/><span class="add">+</span></td>
          <td class="subtotal">{$vo.price*$vo.num}</td>
          <td class="operation"><span>加入收藏夹</span><br><span class="delete del" attr-id="{$vo.id}">删除</span></td>
        </tr>
      {/volist}
      </tbody>
    </table>
      
    <div class="foot" id="foot">
      <label class="fl select-all"><input type="checkbox" class="check-all check"/>&nbsp;全选</label>
      <div class="fr closing">去结算</div>
      <div class="fr total">合计：￥<span id="priceTotal">0.00</span></div>
      <div class="fr selected" id="selected">已选商品<span id="selectedTotal">0</span>件<span class="arrow up">︽</span><span class="arrow down">︾</span></div>
    </div>
</div>
<script>
$(function(){

  //删除单件商品
  $('.operation .del').click(function(){
    var conf = confirm('确定删除此商品吗？');
    if (conf) {
      var id = $(this).attr('attr-id');
      var url = "{:url('goods/delete_car')}";
      var data = {'id' : id};
      $.post(url,data,function(result){
        if (result.status == 1) {
          $('#cartTable tbody').empty();
          data = result.data;
          var html = '';
          $(data).each(function(){
            html += '<tr><td class="checkbox"><input class="check-one check" type="checkbox"/></td><td class="goods"><img src="'+this.picture+'" alt=""/><span>'+this.gname+'</span></td><td class="price">'+this.price+'</td><td class="count"><span class="reduce"></span><input class="count-input" type="text" value="'+this.num+'"/><span class="add">+</span></td><td class="subtotal">'+this.price*this.num+'</td><td class="operation"><span>加入收藏夹</span><br><span class="delete del" attr-id="'+this.id+'">删除</span></td></tr>';
          });
          $('#cartTable tbody').html(html);
        } else if(result.status == 0){
          alert(result.message);
        }
      },'json');
    }
  });

  $('.foot .closing').click(function(){
    if (parseInt($('#selectedTotal').html()) == 0) {
      alert('请先选择结算的商品！');return;
    } else{
      var ids = '';
      var num = '';
      var picture = '';
      var price = '';
      var subtotal = '';
      var gname = '';
      $('.checkbox input').each(function(){
        if ($(this).attr("checked")) { 
          ids += $(this).attr('value') + ',';
          num += $(this).parent().siblings('.count').children('.count-input').val() + ',';
          picture += $(this).parent().siblings('.goods').children('img').attr('src')+',';
          price += $(this).parent().siblings('.price').html()+',';
          subtotal += $(this).parent().siblings('.subtotal').html()+',';
          gname += $(this).parent().siblings('.goods').children('span').html() + ','; 
        }
      });
      var url = "{:url('order/add_order')}";
      data = {
        'ids' : ids,
        'num' : num,
        'picture' : picture,
        'price' : price,
        'subtotal' : subtotal,
        'gname' : gname,
      };
      $.post(url,data,function(result){
        if (result.code == 1) {
          // console.log(result.data);
          location.href=result.data;
        } else if(result.code == 0){
          alert(result.msg);
        }
      })
    }
  }); 
});
</script>
{include file="common/footer" /}
</body>
</html>
